package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述：
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public void saveAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.saveAddressBook(addressBook);
    }

    @Override
    public List<AddressBook> getAllAddressBook() {
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> addressBooks=addressBookMapper.getAllAddressBook(userId);
        return addressBooks;
    }

    @Override
    public AddressBook getDefaultAddressBook() {
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = addressBookMapper.getDefaultAddressBook(userId);
        return addressBook;
    }

    @Override
    public AddressBook getAddressBookById(Long id) {
        AddressBook addressBook=addressBookMapper.getAddressBookById(id);
        return addressBook;
    }

    @Override
    public void updateAddressBook(AddressBook addressBook) {
        addressBookMapper.updateAddressBook(addressBook);
    }

    @Override
    public void deleteAddressBookById(Long id) {
        addressBookMapper.deleteAddressBookById(id);
    }

    @Override
    public void setDefaultAddressBook(AddressBook addressBook) {
        //1.将当前用户的所有地址设置为非默认
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.cancelDefault(addressBook);

        //2.将当前地址设置为默认
        addressBook.setIsDefault(1);
        addressBookMapper.updateAddressBook(addressBook);
    }
}
